package com.cmc.cloud.cmclink.doc.controller.admin.si;

import java.util.Collection;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.validation.Valid;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.cmc.cloud.cmclink.doc.api.document.emums.CostTypeEnum;
import com.cmc.cloud.cmclink.doc.constants.UrlConstants;
import com.cmc.cloud.cmclink.doc.convert.BlnoFreightConvert;
import com.cmc.cloud.cmclink.doc.entity.BlnoFreightDO;
import com.cmc.cloud.cmclink.doc.service.si.BlnoFreightService;
import com.cmc.cloud.cmclink.doc.util.CostChargeWithBlnoUtil;
import com.cmc.cloud.cmclink.doc.vo.blnochargevo.BlnoLocalChargeResultVO;
import com.cmc.cloud.cmclink.doc.vo.blnofreightvo.*;
import com.cmes.framework.biz.advice.annotations.UseResponsePropertyAdvice;
import com.cmes.framework.common.pojo.CommonResult;
import com.cmes.framework.operatelog.core.annotations.OperateLog;
import com.cmes.framework.operatelog.core.enums.OperateTypeEnum;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;

/**
 * 海运费附加费前端接口Controller
 *
 * @author codegen
 */
@Tag(name = "管理后台 - 海运费附加费")
@RestController
@Validated
@RequestMapping(UrlConstants.APPLICATION_URL + "/blnoFreight")
public class BlnoFreightController {

    @Resource
    private BlnoFreightService blnoFreightService;

    @Resource
    private CostChargeWithBlnoUtil costChargeWithBlnoUtil;

    @Operation(summary = "创建海运费附加费")
    @OperateLog(type = OperateTypeEnum.CREATE)
    @PostMapping("/create")
    public CommonResult<Void> createBlnoFreight(@Valid @RequestBody BlnoFreightCreateReqVO createReqVO) {
        blnoFreightService.createBlnoFreight(createReqVO);
        return CommonResult.success();
    }

    @Operation(summary = "更新海运费附加费")
    @OperateLog(type = OperateTypeEnum.UPDATE)
    @PostMapping("/update")
    public CommonResult<Void> updateBlnoFreight(@Valid @RequestBody BlnoFreightUpdateReqVO updateReqVO) {
        blnoFreightService.updateBlnoFreight(updateReqVO);
        return CommonResult.success();
    }

    @Operation(summary = "删除海运费附加费")
    @OperateLog(type = OperateTypeEnum.DELETE)
    @PostMapping("/delete/{id}")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Void> deleteBlnoFreight(@PathVariable("id") Long id) {
        blnoFreightService.deleteBlnoFreight(id);
        return CommonResult.success();
    }

    @Operation(summary = "获得海运费详情列表")
    @OperateLog(type = OperateTypeEnum.GET)
    @GetMapping("/get/{id}")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @UseResponsePropertyAdvice
    public CommonResult<BlnoFreightAllRespVO> getBlnoAllFreight(@PathVariable("id") Long id) {
        BlnoFreightAllRespVO freightResp = blnoFreightService.getBlnoAllFreight(id);
        return CommonResult.success(freightResp);
    }

    @Operation(summary = "获得海运费附加费列表")
    @OperateLog(type = OperateTypeEnum.GET)
    @GetMapping("/list")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    public CommonResult<List<BlnoFreightRespVO>> getBlnoFreightList(@RequestParam("ids") Collection<Long> ids) {
        List<BlnoFreightDO> list = blnoFreightService.getBlnoFreightList(ids);
        return CommonResult.success(BlnoFreightConvert.INSTANCE.convertList(list));
    }

    @Operation(summary = "获得所有提单列表")
    @OperateLog(type = OperateTypeEnum.GET)
    @PostMapping("/list/bill")
    @UseResponsePropertyAdvice
    @PreAuthorize("@ss.hasPermission('doc:caculatefreightpol:search')")
    public CommonResult<List<BlnoFreightCalRespVO>> getBlnoFreightList(@Valid @RequestBody BlnoFreightCalReqVO reqVO) {
        List<BlnoFreightCalRespVO> list = blnoFreightService.getBlnoWithFreightList(reqVO);
        return CommonResult.success(list);
    }

    @Operation(summary = "多式联运查询Blno info")
    @PostMapping("/list/multi")
    @UseResponsePropertyAdvice
    @OperateLog(name = "multi freight 查询", type = OperateTypeEnum.GET)
    @PreAuthorize("@ss.hasPermission('doc:calculateShippingCost:search')")
    public CommonResult<List<MultiBlnoFreightRespVO>>
        getMultiBlnoInfo(@Valid @RequestBody MultiBlnoFreightReqVO multiBlnoFreightReqVO) {
        return CommonResult.success(blnoFreightService.getMultiBlnoInfo(multiBlnoFreightReqVO));
    }

    @Operation(summary = "预计算运费")
    @OperateLog(type = OperateTypeEnum.UPDATE)
    @GetMapping("/cal/forecast")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('doc:caculatefreightpol:Forecast')")
    public CommonResult<List<BlnoLocalChargeResultVO>> calForecastFreight(@RequestParam("ids") Collection<Long> ids) {
        Map<Long, String> errMsgMap = blnoFreightService.calForecastFreight(ids);
        List<BlnoLocalChargeResultVO> rspList =
            costChargeWithBlnoUtil.transitMapToList(ids, errMsgMap, CostTypeEnum.FREIGHT_CAL.getValue());
        return CommonResult.success(rspList);
    }

    @Operation(summary = "计算运费")
    @OperateLog(type = OperateTypeEnum.UPDATE)
    @GetMapping("/cal/ocean")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission({'doc:caculatefreightpol:Freight','doc:calculateShippingCost:Freight'})")
    public CommonResult<List<BlnoLocalChargeResultVO>> calOceanFreight(@RequestParam("ids") Collection<Long> ids) {
        Map<Long, String> errMsgMap = blnoFreightService.calOceanFreight(ids);
        List<BlnoLocalChargeResultVO> rspList =
            costChargeWithBlnoUtil.transitMapToList(ids, errMsgMap, CostTypeEnum.FREIGHT_CAL.getValue());
        return CommonResult.success(rspList);
    }

    @Operation(summary = "发送财务")
    @OperateLog(type = OperateTypeEnum.OTHER)
    @GetMapping("/account")
    @PreAuthorize("@ss.hasPermission({'doc:caculatefreightpol:Account','doc:calculateShippingCost:Account'})")
    public CommonResult<List<BlnoLocalChargeResultVO>> toAccountFreight(@RequestParam("ids") Collection<Long> ids) {
        Map<Long, String> errMsgMap = blnoFreightService.toAccountFreight(ids);
        List<BlnoLocalChargeResultVO> rspList =
            costChargeWithBlnoUtil.transitMapToList(ids, errMsgMap, CostTypeEnum.FREIGHT_ACCOUNT.getValue());
        return CommonResult.success(rspList);
    }
}
